//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_issue_api_serv

import (
	"context"
	"fmt"
	"time"

	"atomgit.com/openlinksaas/api-server/api_common"
	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/issue_dao"
	"atomgit.com/openlinksaas/api-server/dao/project_dao"
	"atomgit.com/openlinksaas/api-server/dao/project_entry_dao"
	"atomgit.com/openlinksaas/api-server/dao/user_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_issue"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_issue_api"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
	"google.golang.org/protobuf/types/known/anypb"
)

func (apiImpl *ProjectIssueApiImpl) addCreateEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CreateRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueId string, nowTime int64) {
	eventData, err := anypb.New(&events_issue.CreateEvent{
		IssueId:   issueId,
		IssueType: int32(req.IssueType),
		Title:     req.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if req.IssueType == project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	dbSess, err := dao.StartSession()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       nowTime,
			RefType:         refType,
			RefId:           issueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})

}

func (apiImpl *ProjectIssueApiImpl) addRemoveEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.RemoveRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.RemoveEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})

}

func (apiImpl *ProjectIssueApiImpl) addUpdateEvent(
	sessionItem *dao.SessionDbItem,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	newTitle string) {
	eventData, err := anypb.New(&events_issue.UpdateEvent{
		IssueId:   issueItem.IssueId,
		IssueType: issueItem.IssueType,
		OldTitle:  issueItem.BasicInfo.Title,
		NewTitle:  newTitle,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       issueItem.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           issueItem.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addUpdateTagEvent(
	sessionItem *dao.SessionDbItem,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	oldTagList, newTagList []*events_issue.TagInfo,
) {
	eventData, err := anypb.New(&events_issue.UpdateTagEvent{
		IssueId:    issueItem.IssueId,
		IssueType:  issueItem.IssueType,
		Title:      issueItem.BasicInfo.Title,
		OldTagList: oldTagList,
		NewTagList: newTagList,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       issueItem.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           issueItem.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addAssignExecUserEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.AssignExecUserRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	userItem *user_dao.UserInfoDbItem) {
	eventData, err := anypb.New(&events_issue.AssignExecUserEvent{
		IssueId:             issueItem.IssueId,
		IssueType:           issueItem.IssueType,
		Title:               issueItem.BasicInfo.Title,
		ExecUserId:          req.ExecUserId,
		ExecUserDisplayName: userItem.BasicInfo.DisplayName,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addAssignCheckUser(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.AssignCheckUserRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	userItem *user_dao.UserInfoDbItem) {
	eventData, err := anypb.New(&events_issue.AssignCheckUserEvent{
		IssueId:              issueItem.IssueId,
		IssueType:            issueItem.IssueType,
		Title:                issueItem.BasicInfo.Title,
		CheckUserId:          req.CheckUserId,
		CheckUserDisplayName: userItem.BasicInfo.DisplayName,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addChangeStateEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.ChangeStateRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.ChangeStateEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
		OldState:  issueItem.State,
		NewState:  int32(req.State),
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addUpdateProcessStageEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.UpdateProcessStageRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.UpdateProcessStageEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
		OldStage:  issueItem.ProcessStage,
		NewStage:  int32(req.ProcessStage),
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addLinkSpritEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.LinkSpritRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	entryItem *project_entry_dao.EntryInfoDbItem) {
	eventData, err := anypb.New(&events_issue.LinkSpritEvent{
		IssueId:    req.IssueId,
		IssueType:  issueItem.IssueType,
		Title:      issueItem.BasicInfo.Title,
		SpritId:    req.SpritId,
		SpritTitle: entryItem.EntryTitle,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCancelLinkSpritEvent(
	sessionItem *dao.SessionDbItem,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	entryItem *project_entry_dao.EntryInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CancelLinkSpritEvent{
		IssueId:    issueItem.IssueId,
		IssueType:  issueItem.IssueType,
		Title:      issueItem.BasicInfo.Title,
		SpritId:    entryItem.EntryId,
		SpritTitle: entryItem.EntryTitle,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       issueItem.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           issueItem.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addSetStartTimeEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.SetStartTimeRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.SetStartTimeEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
		StartTime: req.StartTime,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCancelStartTimeEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CancelStartTimeRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CancelStartTimeEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addSetEndTimeEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.SetEndTimeRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.SetEndTimeEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
		EndTime:   req.EndTime,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCancelEndTimeEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CancelEndTimeRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CancelEndTimeEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addSetEstimateMinutesEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.SetEstimateMinutesRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.SetEstimateMinutesEvent{
		IssueId:         req.IssueId,
		IssueType:       issueItem.IssueType,
		Title:           issueItem.BasicInfo.Title,
		EstimateMinutes: req.EstimateMinutes,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCancelEstimateMinutesEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CancelEstimateMinutesRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CancelEstimateMinutesEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addSetRemainMinutesEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.SetRemainMinutesRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.SetRemainMinutesEvent{
		IssueId:         req.IssueId,
		IssueType:       issueItem.IssueType,
		Title:           issueItem.BasicInfo.Title,
		RemainMinutes:   req.RemainMinutes,
		HasSpendMinutes: req.HasSpendMinutes,
		SpendMinutes:    req.SpendMinutes,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCancelRemainMinutesEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CancelRemainMinutesRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CancelRemainMinutesEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCreateSubIssueEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CreateSubIssueRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CreateSubIssueEvent{
		IssueId:    req.IssueId,
		IssueType:  issueItem.IssueType,
		IssueTitle: issueItem.BasicInfo.Title,
		Title:      req.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addUpdateSubIssueEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.UpdateSubIssueRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	subIssueItem *issue_dao.SubIssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.UpdateSubIssueEvent{
		IssueId:    req.IssueId,
		IssueType:  issueItem.IssueType,
		IssueTitle: issueItem.BasicInfo.Title,
		OldTitle:   subIssueItem.BasicInfo.Title,
		NewTitle:   req.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addUpdateSubIssueStateEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.UpdateSubIssueStateRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	subIssueItem *issue_dao.SubIssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.UpdateSubIssueStateEvent{
		IssueId:    req.IssueId,
		IssueType:  issueItem.IssueType,
		IssueTitle: issueItem.BasicInfo.Title,
		Title:      subIssueItem.BasicInfo.Title,
		Done:       req.Done,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addRemoveSubIssueEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.RemoveSubIssueRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem,
	subIssueItem *issue_dao.SubIssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.RemoveSubIssueEvent{
		IssueId:    req.IssueId,
		IssueType:  issueItem.IssueType,
		IssueTitle: issueItem.BasicInfo.Title,
		Title:      subIssueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addAddDependenceEvent(
	sessionItem *dao.SessionDbItem,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem, dependIssueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.AddDependenceEvent{
		IssueId:          issueItem.IssueId,
		IssueType:        issueItem.IssueType,
		IssueTitle:       issueItem.BasicInfo.Title,
		DependIssueId:    dependIssueItem.IssueId,
		DependIssueType:  dependIssueItem.IssueType,
		DependIssueTitle: dependIssueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       projectItem.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           issueItem.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addRemoveDependenceEvent(
	sessionItem *dao.SessionDbItem,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem, dependIssueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.RemoveDependenceEvent{
		IssueId:          issueItem.IssueId,
		IssueType:        issueItem.IssueType,
		IssueTitle:       issueItem.BasicInfo.Title,
		DependIssueId:    dependIssueItem.IssueId,
		DependIssueType:  dependIssueItem.IssueType,
		DependIssueTitle: dependIssueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       projectItem.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           issueItem.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addSetDeadLineTimeEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.SetDeadLineTimeRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.SetDeadLineTimeEvent{
		IssueId:      req.IssueId,
		IssueType:    issueItem.IssueType,
		Title:        issueItem.BasicInfo.Title,
		DeadLineTime: req.DeadLineTime,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectIssueApiImpl) addCancelDeadLineTimeEvent(
	sessionItem *dao.SessionDbItem,
	req *project_issue_api.CancelDeadLineTimeRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	issueItem *issue_dao.IssueInfoDbItem) {
	eventData, err := anypb.New(&events_issue.CancelDeadLineTimeEvent{
		IssueId:   req.IssueId,
		IssueType: issueItem.IssueType,
		Title:     issueItem.BasicInfo.Title,
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	eventType := events_api.EVENT_TYPE_EVENT_TYPE_TASK
	refType := events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_TASK
	if issueItem.IssueType == int32(project_issue_api.ISSUE_TYPE_ISSUE_TYPE_BUG) {
		eventType = events_api.EVENT_TYPE_EVENT_TYPE_BUG
		refType = events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_BUG
	}
	eventTime := time.Now().UnixNano() / 1e6
	dbSess, err := dao.StartSession()
	if err != nil {
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       eventType,
			EventTime:       eventTime,
			RefType:         refType,
			RefId:           req.IssueId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}
